package com.naiterui.ehp.bs.recommend.controller.inner;

import com.naiterui.ehp.bp.bo.recommend.*;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IDrDiagnosisService;
import com.naiterui.ehp.bs.recommend.util.PatientIntelligentLabelUtil;
import com.naiterui.ehp.bs.recommend.vo.param.DiagnosisVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 诊断相关内部接口
 *
 * @since 2.14
 */
@RequestMapping("recommend/inner/diagnosis")
@Controller
@ApiIgnore
public class InnerDiagnosisController {
    private static final Logger LOGGER = LoggerFactory.getLogger(InnerDiagnosisController.class);

    @Autowired
    private IDrDiagnosisService diagnosisService;

    /**
     * InnerDiagnosisController.getDiagnosisList()
     *
     *
     * @return
     *
     * @throws Exception
     * @Date 2017年12月20日
     * @since 2.14
     */

    @RequestMapping("update")
    @ResponseBody
    public ResponseVO<List<DiagnosisBO>> getDiagnosisList(@RequestBody DiagnosisParamBO diagnosisParamVO) throws Exception {
        LOGGER.info("------medicationRecom getDiagnosisList param; diagnosisParamVO : {}", diagnosisParamVO);
        if (diagnosisParamVO == null || diagnosisParamVO.getDoctorId() == null) {
            LOGGER.info("更新临床诊断列表，医生ID为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        List<String> diagnosisList = diagnosisParamVO.getDiagnosis();
        if (CollectionUtils.isEmpty(diagnosisList)) {
            LOGGER.info("更新临床诊断列表，诊断信息为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        List<DiagnosisVO> diagnosisVOList = diagnosisService.updateCommonDiagnosis(diagnosisParamVO.getDoctorId(), diagnosisList);
        List<DiagnosisBO> diagnosisBOList = new ArrayList<>();
        BeanUtil.copyList(diagnosisVOList, diagnosisBOList, DiagnosisBO.class);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, diagnosisBOList);
    }


    /**
     * 获取非标准诊断
     *
     * @param recomDiagnosisParamBO
     *
     * @return
     *
     * @throws Exception
     */

    @RequestMapping("standard/list")
    @ResponseBody
    public ResponseVO<RecomDiagnosisBO> getStandardDiagnosisList(@RequestBody RecomDiagnosisParamBO recomDiagnosisParamBO) throws Exception {
        LOGGER.info("------medicationRecom getStandardDiagnosisList param; recomDiagnosisParamBO : {}", recomDiagnosisParamBO);
        if (recomDiagnosisParamBO == null || CollectionUtils.isEmpty(recomDiagnosisParamBO.getDiagnosis())) {
            LOGGER.info("临床诊断列表为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        RecomDiagnosisBO recomDiagnosisBO = diagnosisService.getStandardDiagnosis(recomDiagnosisParamBO.getDiagnosis());
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, recomDiagnosisBO);
    }


    /**
     * 获取患者最新的诊断
     * @return
     * @throws Exception
     */
    @PostMapping("patient/lastest/get")
    @ResponseBody
    public List<String> patientLast(Long doctorId, Long inquirerId) throws Exception {
        LOGGER.info("获取患者最新的诊断： doctorId={}, patientId= {}", doctorId, inquirerId);
        return PatientIntelligentLabelUtil.getDiagnosis(doctorId, inquirerId);
    }

    /**
     * 更新患者最新的诊断
     * @return
     * @throws Exception
     */
    @PostMapping("patient/lastest/update")
    @ResponseBody
    public void patientLastUpdate(@RequestBody PatientDiagnisisBO diagnisisBO) throws Exception {
        LOGGER.info("更新患者最新的诊断: diagnisisBO={}", diagnisisBO.toString());
        PatientIntelligentLabelUtil.putDiagnosis(diagnisisBO.getDoctorId(), diagnisisBO.getInquirerId(), diagnisisBO.getDiagnosis());
    }

    /**
     * 根据名称获取诊断列表
     * @param names
     * @return
     * @throws BusinessException
     */
    @PostMapping("/name/list")
    @ResponseBody
    public ResponseVO<List<SuperviseDiagnosisBO>> diagnosiss(@RequestBody List<String> names) {
        return PublicService.returnResponseVO(diagnosisService.diagnosiss(names));
    }
}
